const mongoose = require('mongoose')
const Joi = require('joi');
const userSchema = new mongoose.Schema({
    user_name: {
        type: String,
        required: [true, "未输入姓名"],
        minlength: [2, "用户名长度不足"],
        maxlength: [8, "用户名长度过长"],
    },
    user_phone: {
        type: Number,
        required: [true, "未输入电话"],
    },
    user_password: {
        type: String,
        required: [true, "未输入密码"],
    },
    user_createTime: {
        type: Date,
        default: Date.now
    },
    user_static: {
        type: Number,
        enum: [0, 1],
        default: 1,
    },
    user_role: {
        type: String,
        enum: ["admin", "normal"],
        default: "normal"
    },
});

const JoiSchema = Joi.object({
    user_name: Joi.string().min(2).max(8).required().error(new Error("姓名格式不正确")),
    user_phone: Joi.string().pattern(new RegExp('^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$')).required().error(new Error("手机格式不正确")),
    user_password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{6,20}$')).error(new Error("密码不正确")),
    user_createTime: Joi.date(),
    user_static: Joi.number().integer(),
    user_role: Joi.string()
});

const User = mongoose.model("User", userSchema);
// const test = new User({
//     user_name: "测试者1",
//     user_phone: "13111111111",
//     user_password: "11111111",
// });
// test.save();

module.exports = User;